﻿using System;
using System.IO;

using Commons = NougakudoCompanion.Commons;

namespace NougakuDoController
{
    class Downloader
    {
        Commons.Runtime runtime;

        internal Downloader(Commons.Runtime runtime)
        {
            this.runtime = runtime; 
        }

        string ErrorMsg { set; get; }

        /// <summary>
        /// Download zip files from azure blob storage sevive.
        /// </summary>
        /// <returns></returns>
        internal Tuple<bool, string> Start()
        {
            ErrorMsg = "";

            foreach (var i in runtime.Downloads)
            {
                string name = i + ".zip";
#if DEBUG
                Logging.Write("Downloader:Download start for {0} in blob.", new object[] { name }, Logging.LogType.INFO);
#endif

                var ret = Download(name);
                if (!ret)
                {
                    Logging.Write("Downloader:Not found {0} in blob :{1}", new object[] { name, ErrorMsg }, Logging.LogType.ERROR);
                    return new Tuple<bool,string>(false, ErrorMsg);
                }
                Logging.Write("Downloader:Download completed for {0} in blob.", new object[] { name }, Logging.LogType.INFO);

            }
            return new Tuple<bool,string>(true, "");
        }

        /// <summary>
        /// Download a configuration.xml from Azure blob storage service
        /// </summary>
        /// <returns>option: download new configuration from blob.</returns>
        internal Tuple<bool, string> GetConfiguration(string newFile = "")
        {
            ErrorMsg = "";

            Logging.Write("Downloader:Download started for {0} in blob.", new object[] { Commons.Constants.CONFIGURATION }, Logging.LogType.INFO);
            var ret = Download(Commons.Constants.CONFIGURATION, newFile);
            if (!ret)
            {
                Logging.Write("Downloader:Not found {0} in blob :{1}", new object[] { Commons.Constants.CONFIGURATION, ErrorMsg }, Logging.LogType.ERROR);
                return new Tuple<bool,string>(false, ErrorMsg);
            }
            Logging.Write("Downloader:Download completed for {0} in blob.", new object[] { Commons.Constants.CONFIGURATION }, Logging.LogType.INFO);

            return new Tuple<bool,string>(true, "");
        }

        bool Download(string name, string file = "")
        {
            if (file == "") file = name;

            var ret = Commons.BlobStorageHelper.DownloadBlob(
                        runtime.Environment, name, 
                        Path.Combine(runtime.Environment.DownloadFolder, file));
            ErrorMsg = ret.Item2;
            if (!ret.Item1)
                DeleteFile(Path.Combine(runtime.Environment.DownloadFolder, file));
            return ret.Item1;
        }

        void DeleteFile(string file)
        {
            try
            {
                File.Delete(file);
            }
            catch {}
        }
    }
}
